package com.example.txjc.Activity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.txjc.Util.ProjectConfig;
import com.example.txjc.R;


import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2017/9/29.
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends Activity {
    @ViewInject(R.id.activity_login_et_username)
    private EditText mEditText_userName;
    @ViewInject(R.id.activity_login_et_password)
    private EditText mEditText_password;
    @ViewInject(R.id.checkPwd)
    private CheckBox checkPwd;
    @ViewInject(R.id.activity_login_btn_login)
    private Button mButton_login;
    private boolean ischecked=true;
    private SharedPreferences sp;
    private String username;
    private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ActionBar bar = getSupportActionBar();
  //      bar.hide();
         x.view().inject(this);
        sp=this.getSharedPreferences("userInfo", this.MODE_PRIVATE);
        username=sp.getString("username", "");
        password=sp.getString("pwd","");
        ischecked=sp.getBoolean("ischecked", false);
        if(username!=null&&password!=null)
        {
            Log.e("userName",username);

            if (ischecked)
            {
                if (!username.equals(""))
                {
                    mEditText_userName.setText(username);
                }

                if (!password.equals(""))
                {
                    mEditText_password.setText(password);
                }

                checkPwd.setChecked(true);
            }
            else
            {
                //checkPwd.setChecked(false);
            }
        }
    }
    @Event(R.id.activity_login_btn_login)
    private void btnClick(View v){
//        final LoadingDialog dialog = new LoadingDialog(LoginActivity.this);
//        dialog.show();
        final String username = mEditText_userName.getText().toString();
       final  String password = mEditText_password.getText().toString();
        /*MyApplication app = (MyApplication)getApplication();
        app.setUserName(username);
        app.setPassWord(password);
        if(username.equals("")||password.equals("")){
            ToastUtil.show(LoginActivity.this,"请输入用户名或者密码");
            return;
        }*/
        RequestParams params = new RequestParams(ProjectConfig.SERVER_URL+"login");
        params.addBodyParameter("username",username);
        params.addBodyParameter("password",password);

    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
